﻿$(function () {
    if ($("input[id$='hdfTagKey']").val() == null || $("input[id$='hdfTagKey']").val() == "") {
        InitializeSelect("");
    }
    else {
        UploadSelect();
    }
});
function AjaxJSON(action, nodeId, tagId, callback) {
    $.getJSON("MediaController.ashx", { ContentType: "json", Action: action, NodeId: nodeId, TagId: tagId },
         function (data) {
             callback(data);
         });
}
function InitializeSelect(nodeKey) {

    AjaxJSON("GetChildNodes", nodeKey, "", function (data) {
        if (data == null) { return; }
        data = eval(data);
        var index = $("select").size();
        var len = data.length;
        if (len > 0) {
            $("<select id='no" + index + "' onchange='show(event)'></select>").appendTo("#Category");
            $("<option value='-1'>请选择</option>").appendTo("#no" + index + "");
            for (var i = 0; i < len; i++) {
                $("<option value=" + data[i].Key + ">" + data[i].Name + "</option>").appendTo("#no" + index + "");
            }
        }

    });
}
function show(event) {
    var event = window.event || event;


    var obj = event.srcElement;

    if (obj == null)
        obj = event.target;

    var sNext = $(obj).nextAll("select");
    sNext.each(function (i) {
        $(this).remove();
    });

    $("input[id$='hdfCategory']").val(obj.value);

    InitializeSelect(obj.value);
}
function UploadSelect() {
    var tagKey = $("input[id$='hdfTagKey']").val();
    AjaxJSON('GetTag', "", tagKey, function (data) {
        if (data == null) { return; }
        data = eval(data);
        $("#Category").append("<div id=EditSelect><strong>" + data[0].Name + "</strong>[<a style=\"cursor:pointer;\"  onclick='EditSelect()'>修改</a>]</div>");

    });
}
function EditSelect() {
    $("#Category #EditSelect").remove();
    $("input[id$='hdfCategory']").val("");
    InitializeSelect("");
}